package com.ztorn.fiscale.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ztorn.common.core.utils.poi.ExcelUtil;
import com.ztorn.common.core.web.controller.BaseController;
import com.ztorn.common.core.web.domain.AjaxResult;
import com.ztorn.common.core.web.page.TableDataInfo;
import com.ztorn.common.log.annotation.Log;
import com.ztorn.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ztorn.fiscale.domain.FiscaleDocument;
import com.ztorn.fiscale.service.IFiscaleDocumentService;

/**
 * 单证Controller
 *
 * @author ztorn
 */
@RestController
@RequestMapping("/document")
public class FiscaleDocumentController extends BaseController
{
    @Autowired
    private IFiscaleDocumentService fiscaleDocumentService;

    /**
     * 查询单证列表
     */
    @PreAuthorize("@ss.hasPermi('fiscale:document:list')")
    @GetMapping("/list")
    public TableDataInfo list(FiscaleDocument fiscaleDocument)
    {
        startPage();
        List<FiscaleDocument> list = fiscaleDocumentService.selectFiscaleDocumentList(fiscaleDocument);
        return getDataTable(list);
    }

    /**
     * 导出单证列表
     */
    @PreAuthorize("@ss.hasPermi('fiscale:document:export')")
    @Log(title = "单证", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, FiscaleDocument fiscaleDocument)
    {
        List<FiscaleDocument> list = fiscaleDocumentService.selectFiscaleDocumentList(fiscaleDocument);
        ExcelUtil<FiscaleDocument> util = new ExcelUtil<FiscaleDocument>(FiscaleDocument.class);
        util.exportExcel(response, list, "单证数据");
    }

    /**
     * 获取单证详细信息
     */
    @PreAuthorize("@ss.hasPermi('fiscale:document:query')")
    @GetMapping(value = "/{documentId}")
    public AjaxResult getInfo(@PathVariable("documentId") Long documentId)
    {
        return AjaxResult.success(fiscaleDocumentService.selectFiscaleDocumentById(documentId));
    }

    /**
     * 新增单证
     */
    @PreAuthorize("@ss.hasPermi('fiscale:document:add')")
    @Log(title = "单证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FiscaleDocument fiscaleDocument)
    {
        return toAjax(fiscaleDocumentService.insertFiscaleDocument(fiscaleDocument));
    }

    /**
     * 修改单证
     */
    @PreAuthorize("@ss.hasPermi('fiscale:document:edit')")
    @Log(title = "单证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FiscaleDocument fiscaleDocument)
    {
        return toAjax(fiscaleDocumentService.updateFiscaleDocument(fiscaleDocument));
    }

    /**
     * 删除单证
     */
    @PreAuthorize("@ss.hasPermi('fiscale:document:remove')")
    @Log(title = "单证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{documentIds}")
    public AjaxResult remove(@PathVariable Long[] documentIds)
    {
        return toAjax(fiscaleDocumentService.deleteFiscaleDocumentByIds(documentIds));
    }

    /**
     * 上传单证文件
     */
    @PreAuthorize("@ss.hasPermi('fiscale:document:upload')")
    @Log(title = "单证上传", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file,
                             @RequestParam("documentTypeId") Long documentTypeId,
                             @RequestParam("referenceNo") String referenceNo)
    {
        try
        {
            // 添加日志记录
            System.out.println("收到上传请求: " + file.getOriginalFilename() + ", 类型ID: " + documentTypeId + ", 关联单号: " + referenceNo);
            
            if (file.isEmpty()) {
                return AjaxResult.error("上传文件不能为空");
            }
            
            if (documentTypeId == null) {
                return AjaxResult.error("单证类型ID不能为空");
            }
            
            FiscaleDocument document = fiscaleDocumentService.uploadDocument(file, documentTypeId, referenceNo);
            return AjaxResult.success("上传成功", document);
        }
        catch (Exception e)
        {
            e.printStackTrace(); // 添加堆栈跟踪
            return AjaxResult.error("上传失败：" + e.getMessage());
        }
    }
    
    /**
     * 测试上传接口是否可访问
     */
    @GetMapping("/uploadTest")
    public AjaxResult uploadTest()
    {
        return AjaxResult.success("上传接口可访问");
    }
}
